package com.easylinkin.linkappapi.lobar.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.common.service.OSSService;
import com.easylinkin.linkappapi.common.utils.excel.ExcelReadUtil;
import com.easylinkin.linkappapi.lobar.entity.emp.EmpUserBase;
import com.easylinkin.linkappapi.lobar.mapper.EmpUserBaseMapper;
import com.easylinkin.linkappapi.lobar.service.UserProjectService;
import com.easylinkin.linkappapi.quality.entity.Problem;
import com.easylinkin.linkappapi.quality.entity.ProblemType;
import com.easylinkin.linkappapi.quality.mapper.ProblemMapper;
import com.easylinkin.linkappapi.quality.mapper.ProblemTypeMapper;
import com.easylinkin.linkappapi.tenant.entity.LinkappTenant;
import com.easylinkin.linkappapi.tenant.mapper.LinkappTenantMapper;
import java.io.FileInputStream;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @Author: kan yuanfeng
 * @Date: 2022/7/5 16:55
 * @Description:
 */
@RestController
@RequestMapping("sync")
public class TestController {

  @Autowired
  private UserProjectService userProjectService;

  @Autowired
  private EmpUserBaseMapper empUserBaseMapper;

  @Autowired
  private OSSService ossService;

  @Autowired
  private ProblemTypeMapper problemTypeMapper;

  @Autowired
  private ProblemMapper problemMapper;

  @Autowired
  private LinkappTenantMapper linkappTenantMapper;

  @Autowired
  private JdbcTemplate jdbcTemplate;

  @GetMapping("test2")
  public RestMessage test1() throws Exception {
    List<EmpUserBase> empUserBases = empUserBaseMapper.selectList(new QueryWrapper<>());
    empUserBases.forEach(e ->{
      String photo = e.getPhoto();
      if (StringUtils.isNotBlank(photo) && photo.startsWith("data:")){
        String s = ossService.saveImgBase64(photo,e.getName() + "photo.png");
        e.setPhoto(s);
        empUserBaseMapper.updateById(e);
      }
    });
    return RestBuilders.successBuilder().build();
//    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//    FileInputStream fileInputStream = new FileInputStream(
//        "C:\\Users\\77586\\Desktop\\职工名册 (1) (1).xlsx");
//    List<List<String>> list = ExcelReadUtil
//        .getExcelInfo(fileInputStream, 1, 280, false, -1);
//    list.forEach(l ->{
//      EmpUserBaseDTO empUserBaseDTO = new EmpUserBaseDTO();
//      empUserBaseDTO.setEmployStatus(1);
//      String s = l.get(4);
//      try {
//        empUserBaseDTO.setBirthday(format.parse(s.substring(6,10)+"-"+s.substring(10,12)+"-"+s.substring(12,14)));
//      } catch (ParseException e) {
//        e.printStackTrace();
//      }
//      try {
//        empUserBaseDTO.setEmployTime(format.parse(l.get(8)));
//      } catch (ParseException e) {
//        e.printStackTrace();
//      }
//      empUserBaseDTO.setName(l.get(1));
//      empUserBaseDTO.setGender(Integer.valueOf(l.get(2)));
//      empUserBaseDTO.setNation(l.get(3));
//      empUserBaseDTO.setCard(l.get(4));
//      empUserBaseDTO.setTelephone(l.get(5));
//      UserProject userProject = new UserProject();
//      userProject.setJoinTime(empUserBaseDTO.getEmployTime());
//      userProject.setUserName(empUserBaseDTO.getName());
//      userProject.setGateStatus(0);
//      userProject.setStatus(1);
//      userProject.setGender(empUserBaseDTO.getGender());
//      userProject.setBirthday(empUserBaseDTO.getBirthday());
//      userProject.setGroupId(l.get(6));
//      userProject.setNo(l.get(0));
//      userProject.setWorkType(l.get(7));
//      userProject.setIsGroupLeader(0);
//      empUserBaseDTO.setUserProject(userProject);
//      userProjectService.insertEmp(empUserBaseDTO);
//    });
  }

  @GetMapping("test3")
  public RestMessage test2() throws Exception {
    List<ProblemType> problemTypes = problemTypeMapper.selectList(new QueryWrapper<>());
    FileInputStream fileInputStream = new FileInputStream(
        "C:\\Users\\77586\\Desktop\\1_质量问题库.xlsx");
    List<List<String>> list = ExcelReadUtil
        .getExcelInfo(fileInputStream, 1, 280, false, -1);
    list.forEach(l ->{
      ProblemType type = problemTypes.stream()
          .filter(p -> Integer.valueOf(l.get(0)).equals(p.getId()))
          .collect(Collectors.toList()).get(0);
      ProblemType problemType = new ProblemType();
      problemType.setParentId(Integer.valueOf(l.get(0)));
      problemType.setLevel(2);
      problemType.setName(l.get(1));
      problemType.setFullId(type.getId()+"");
      problemType.setFullName(type.getName()+"/"+l.get(1));
      problemType.setCode(l.get(2));
      problemTypeMapper.insert(problemType);
    });
    return RestBuilders.successBuilder().build();
  }

  @GetMapping("test4")
  public RestMessage test4() throws Exception {
    List<ProblemType> problemTypes = problemTypeMapper.selectList(new QueryWrapper<>());
    FileInputStream fileInputStream = new FileInputStream(
        "C:\\Users\\77586\\Desktop\\质量问题库.xlsx");
    List<List<String>> list = ExcelReadUtil
        .getExcelInfo(fileInputStream, 1, 1050, false, -1);
    list.forEach(l ->{
      Problem problem = new Problem();
      problem.setProblemTypeId(Integer.valueOf(l.get(0)));
      problem.setFullId(l.get(1).substring(0,l.get(1).lastIndexOf("/")));
      problem.setFullName(l.get(2));
      problem.setContent(l.get(3));
      problem.setCode(l.get(4));
      problem.setOrder(Integer.valueOf(l.get(5)));
      problem.setLevel(Integer.valueOf(l.get(6)));
      problem.setChangeLimit(Integer.valueOf(l.get(7)));
      problemMapper.insert(problem);
    });
    return RestBuilders.successBuilder().build();
  }



  @GetMapping("test5")
  public RestMessage test5(){
    //分类
    QueryWrapper<ProblemType> queryWrapper1 = new QueryWrapper<>();
    queryWrapper1.isNull("tenant_id_");
    List<ProblemType> problemTypes = problemTypeMapper.selectList(queryWrapper1);
    //问题库
    QueryWrapper<Problem> queryWrapper2 = new QueryWrapper<>();
    queryWrapper2.isNull("tenant_id_");
    List<Problem> problems = problemMapper.selectList(queryWrapper2);
    List<LinkappTenant> list = linkappTenantMapper.selectByProjectId(null);
    list.removeIf(l -> "1".equals(l.getId()));
    list.forEach(l->{
      problemTypes.forEach(p ->{
        ProblemType problemType = BeanUtil.toBean(p, ProblemType.class);
        problemType.setSrcId(p.getId());
        problemType.setId(null);
        problemType.setTenantId(l.getId());
        problemTypeMapper.insert(problemType);
      });
      problems.forEach(p->{
        Problem problem = BeanUtil.toBean(p, Problem.class);
        problem.setSrcId(p.getId());
        problem.setId(null);
        problem.setTenantId(l.getId());
        problemMapper.insert(problem);
      });
    });


//    统一修改
    jdbcTemplate.execute("UPDATE app_problem_type a\n"
        + "left JOIN app_problem_type b\n"
        + "on a.parent_id_ = b.src_id_ and a.tenant_id_ = b.tenant_id_\n"
        + "set a.parent_id_ = b.id\n"
        + "WHERE a.level_ = 2 and a.tenant_id_ is not NULL");

    jdbcTemplate.execute("UPDATE app_problem a\n"
        + "LEFT JOIN app_problem_type b ON a.tenant_id_ = b.tenant_id_ \n"
        + "AND a.problem_type_id_ = b.src_id_ \n"
        + "SET a.problem_type_id_ = b.id \n"
        + "WHERE\n"
        + "\ta.tenant_id_ IS NOT NULL;");

    return RestBuilders.successBuilder().build();
  }

}
